<?php
namespace jy_weishop\page;
defined('BY_JYA') or exit('error');
/*
	页面返回
*/
class order_detail extends \plugin{
	public function getmain($return=array()){
		global $_RQ;
		//返回链接
		$return['forward_url']=base64_encode('jy_weishop/order.list//fromcreate=1');
		$isfollow=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');
		$shopset=$this->plugin_setting('shopset');
		if(!$isfollow && $_RQ['fromcreate'] && !$shopset['noshowqrcode']){
			$return['showwechatcode']=1;
		}
		if($_RQ['id']){
			$id = intval($_RQ['id']);
			$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($_RQ['id'])));
		}elseif($_RQ['ordersn']){
			$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'ordersn'=>trim($_RQ['ordersn'])));
		}
		if(!$order){
			exi('指定订单不存在！','error','goback');
		}
		if($order['status']==0){
			$order['fromseconds']=time()-$order['createtime'];
			$order['outseconds']=$shopset['orderpay_outtime']*3600;
			if($order['fromseconds']<$order['outseconds']){
				$order['fromseconds']=($order['fromseconds']/$order['outseconds'])*428;
				$order['upseconds']=$this->model('model')->date($order['outseconds']-$order['fromseconds']);
			}else{
				$order['upseconds']=0;
			}
		}
		//定义订单状态名称
		$order['statustitle']=$this->order_status[$order['status']];
		if($order['status']==0 && $order['paytype']!='hdfk'){
			$order['statusdesc']='超时未支付，订单将自动取消';
		}elseif($order['status']==1 && $order['paytype']=='hdfk'){
			$order['statustitle']='货到付款';
			$order['statusdesc']='请您收到商品后付款';
		}elseif($order['status']==1 && !$order['refundstate']){
			$order['statusdesc']='请您留意订单进度';
		}elseif($order['status']==2 && !$order['refundstate']){
			$order['statusdesc']='请您留意订单进度';
		}elseif($order['status']==3 && !$order['refundstate']){
			$order['statusdesc']='欢迎下次光临！';
		}elseif($order['status']==4 && !$order['refundstate']){
			$order['statusdesc']='欢迎下次光临！';
		}elseif($order['status']==5){
			$order['statusdesc']='请您留意退款进度';
		}elseif($order['status']==5 && $order['oldstatus']>1){
			$order['statusdesc']='请您留意售后进度';
		}elseif($order['status']==6){
			$order['statusdesc']='感谢您的支持和理解，欢迎下次光临！';
		}else{
			$order['statusdesc']='欢迎下次光临！';
		}
		//包含核销商品
		if($order['status']==2 && $order['isverify']==1){
			$order['statustitle']='部分商品已发货';
			$order['statusdesc']='除核销商品，其他商品已发货';
		}elseif($order['status']==2 && $order['isverify']==2){
			$order['statustitle']='等待核销';
			$order['statusdesc']='属核销商品，请核销！';
		}elseif($order['status']==3 && $order['isverify']==2){
			$order['statustitle']='已核销';
			$order['statusdesc']='属核销商品，已核销完成！';
		}
		$order['express']=$this->pdo_get('order_express',array('orderid'=>$order['id'],'opensend_type'=>0,'uniacid'=>$_SESSION['uniacid']));
		$order['express']['express_name']=$this->model('model')->express_types()[$order['express']['express_id']]['name'];
		//时间格式化
		$order['createtime']=date('Y-m-d H:i:s',$order['createtime']);
		$order['paytime']=date('Y-m-d H:i:s',$order['paytime']);
		$order['sendtime']=$order['express']['createtime']?date('Y-m-d H:i:s',$order['express']['createtime']):'';
		$order['finishtime']=date('Y-m-d H:i:s',$order['finishtime']);
		if ($order['userdeleted'] == 2) {
				exi('订单已经被删除!','error');
		}
		//用户地址
		$order['address']=json_decode($order['address'],true);
		if($order['address'] && ($order['address']['name'] || $order['address']['mobile'])){
			$address=$order['address'];
		}else{
			$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
		}
		if(!is_array($address['pcd'])){
			$address['pcd']=json_decode($address['pcd'],true);
		}
		if(!is_array($address['diyfields'])){
			$address['diyfields']=json_decode($address['diyfields'],true);
		}
		$order['addresstext']=model('core/member')->get_address_str($address);
		$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));                       //用户信息
		$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','status','istaocan','goodsid','optionid','price','total','goodssn','productsn'));
		$merchs =array();
		$jy_giftbao=$this->plugin_exists('jy_giftbao');
		foreach($ordergoods as &$gv){
				$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','unit','type','image','title','merchid'));
				if($jy_giftbao){
					$libaolog=pdo_get('jy_giftbao_active_log',array('goods_id'=>$gv['goodsid'],'ordersn'=>$order['ordersn'],'uniacid'=>$_SESSION['uniacid']));
					if($libaolog['id']){
						$goodsinfo['libao']=pdo_get('jy_giftbao_active',array('id'=>$libaolog['active_id'],'uniacid'=>$_SESSION['uniacid']));
						$goodsinfo['libao']['log_id']=$libaolog['id'];
						$goodsinfo['libao']['libaos']=$goodsinfo['libao']['libaos']?json_decode($goodsinfo['libao']['libaos'],true):array();
						$libaolog['active_libaonum']=$goodsinfo['libao']['active_libaonum']=count($goodsinfo['libao']['libaos']);
						$libaolog['active_choosenum']=$goodsinfo['libao']['active_choosenum']=$goodsinfo['libao']['choosenum'];
						foreach($goodsinfo['libao']['libaos'] as &$glb){
							$glb['price']=pdo_getcolumn('jy_giftbao_libao',array('id'=>$glb['id']),'price');
						}
						if($libaolog['libaos']){
							$libaolog['libaos']=json_decode($libaolog['libaos'],true);
							$goodsinfo['libao']['libaos']=$libaolog['libaos'];
						}else{
							$libaolog['libaos']=array();
							$return['showchooselibaos']=true;
							$return['nowlibao']=$goodsinfo['libao'];
						}				
						foreach($libaolog['libaos'] as &$lb){
							$lb['price']=pdo_getcolumn('jy_giftbao_libao',array('id'=>$lb['id']),'price');
						}
					}
					$goodsinfo['libaolog']=$libaolog;
				}
				$goodsinfo['ordergoodsid']=$gv['id'];
				$goodsinfo['refund_id']=$this->pdo_getcolumn('order_refund',array('ordersn'=>$order['ordersn'],'ordergoodsid'=>$gv['id']),'id');
				$goodsinfo['can_refund']=$this->model('order/refund')->check_order_rerefund($order['id'],'',$gv['goodsid']);
				$goodsinfo['total']=$gv['total'];
				$goodsinfo['price']=$gv['price'];
				if($_SESSION['is_tomedia']){
					$goodsinfo['image']=$goodsinfo['image']?tomedia($goodsinfo['image']):'';
				}
				$goodsinfo['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$gv['goodsid']),'weight');
				$gv['title']=$goodsinfo['title'];
				$gv['image']=$goodsinfo['image'];
				$gv['merchid']=$goodsinfo['merchid'];	
				//获取门店
				$stores = array();
				$goodsinfo['storeids'] = json_decode($goodsinfo['storeids'],true);
				if ($goodsinfo['isverify'] == 1) {
					if (empty($goodsinfo['storeids'])) {
						$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3)));
					}else {
						foreach($goodsinfo['storeids'] as $sv){
							$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3),'id'=>$sv['id']));
						}
					}
				}elseif($shopset['openstore'] ==1){
					if (empty($goodsinfo['storeids'])) {
						$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
					}else {
						foreach($goodsinfo['storeids'] as $sv){
							$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id'=>$sv['id']));
						}
					}
				}
				$goodsinfo['store']=$stores;
				//商品规格
				if($gv['optionid']){
					$options=pdo_get('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'id'=>$gv['optionid']),array('title','specs','weight'));
					if($gv['istaocan']){
						$goodsinfo['selespename']=$options['title'];
					}else{
						$specs=$options['specs'];
						$specs=explode("_",$specs);
						foreach($specs as &$spec){
							$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
						}
						$goodsinfo['selespename']=implode("，",$specs);
						$goodsinfo['weight']=$options['weight'];
					}
				}
				$goodsinfo['buytypetitle']=$this->buytypes()[$order['buytype']];
				//获取商户
				if($goodsinfo['merchid']){
					if(!$merchs[$goodsinfo['merchid']]){
						$merchinfo = pdo_get('jy_merch_user',array('id'=>$goodsinfo['merchid'],'uniacid'=>$_SESSION['uniacid']));
						$merchs[$goodsinfo['merchid']] = $merchinfo;
					}
					$merchs[$goodsinfo['merchid']]['goods'][] = $goodsinfo;
					$return['ismerch']=1;
				}else{
					$merchs[0]['goods'][] = $goodsinfo;
				}
				if($order['storeid']){
					$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id'=>$order['storeid']));
				}
		}	
		$return['self']=pdo_get('jy_weishop_order_self',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['id']));
		$order['selfnumber']=$return['self']['number'];
		if($order['selfnumber']){
			$order['selfnumberurl']=custom_url('jy_weishop/verify.selfdetail//id='.$order['id'].'&verifycode='.$order['selfnumber'],true);
			//小程序二维码：
			$url='/jy_weishop/verify/selfdetail/index?id='.$order['id'].'&verifycode='.$order['selfnumber'];
			$pid='verifyid'.$order['id'].'c'.$order['selfnumber'];
			if($_SESSION['wxapp_id']){
				$order['appcode'] = $this->model('model')->get_wxappewm($url,$pid);
			}
		}
		if($order['sendtype']==1){
			if($order['peisong_id']){
				if(!$this->plugin_exists('jy_peisong')){
					exi('请联系管理员开通《配送员》插件！','error');
				}
				$return['peisong']=pdo_get('jy_peisong_member',array('mid'=>$order['peisong_id'],'uniacid'=>$_SESSION['uniacid']));
			}else{
				$return['peisong']=pdo_get('jy_weishop_order_express',array('orderid'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
		$return['merchs']=$merchs;
		//抵扣	
		$return['order_gifts']=$this->pdo_get('order_gift',array('order_id'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
		if($return['order_gifts']){
			$return['order_gifts']['gifts']=json_decode($return['order_gifts']['gifts'],true);
			if($_SESSION['is_tomedia']){
				foreach($return['order_gifts']['gifts'] as &$giv){
					$giv['image']=$giv['image']?tomedia($giv['image']):'';
				}
			}
		}
		$stores_count=count($stores);
		if (!empty($order['diyformdata'])) {
			$order_data = $order['diyformdata'];
		}
		$order['can_refund']=$this->model('order/refund')->check_order_rerefund($order['id']);
		$order['can_cancel']=$this->model('order/refund')->check_order_cancel($order);
		$return['stores_count']=$stores_count;
		if($order['sendtype']==3 && $order['status']==3){
			$order['content']=$this->pdo_getcolumn('order_virtual',array('orderid'=>$order['id'],'uniacid'=>$_SESSION['uniacid']),'content');
		}
		$order['total']=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$id,'uniacid'=>$_SESSION['uniacid']),'total');
		//卡密
		if($order['status']>0 && $order['status']!=4){
			$return['virtual']=$this->pdo_getall('order_virtual',array('orderid'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		if($return['virtual']){
			foreach($return['virtual'] as &$vv){
				$tempinfo=$this->pdo_get('virtual_temp',array('id'=>$vv['virtualid'],'uniacid'=>$_SESSION['uniacid']),array('id','linktitle','linkurl'));
				$vv['linkurl']=$tempinfo['linkurl'];
				$vv['linktitle']=$tempinfo['linktitle'];
			}
		}
		
		$return['address']=$address;
		$return['member']=$member;
		$return['goods']=$ordergoods;
		$return['stores']=$stores;
		$return['order_data']=json_decode($order_data,true);
		if($order['status']>0 && $order['status']!=4){
			$order_verify=$this->pdo_getall('order_verify',array('orderid'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		if($order_verify){
				foreach($order_verify as &$verify){
					$verify['verifycode']=$verify['verify_code'];
					$code_endtime=$verify['endtime']?date('Y-m-d',$verify['endtime']):'';
					if($verify['status']==0 && $verify['endtime'] && $verify['endtime']<TIMESTAMP){
						$verify['status']=2;
						$this->pdo_update('order_verify',array('status'=>2),array('id'=>$verify['id'],'uniacid'=>$_SESSION['uniacid']));
					}else{
						$verify['url']=custom_url('jy_weishop/verify.detail//id='.$order['id'].'&verifycode='.$verify['verify_code'],true);
						//小程序二维码：
						$url='/jy_weishop/verify/detail/index?id='.$order['id'].'&verifycode='.$verify['verify_code'];
						$pid='verifyid'.$order['id'].'c'.$verify['verify_code'];
						$verify['appcode'] = $this->model('model')->get_wxappewm($url,$pid);
					}
				}
		}
		$return['order_verify']=$order_verify;
		if($order['status']>0 && $order['status']!=4){
			$goods_verify=$this->pdo_getall('verifygoods',array('orderid'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		if($goods_verify){
				foreach($goods_verify as &$gvv){
					if (!(empty($gvv['limitnum']))){
						$verifygoodlogs = pdo_getall('jy_weishop_verifygoods_log',array('verifygoodsid'=>$gvv['id'],'uniacid'=>$_SESSION['uniacid']));
						$verifynum = 0;
						foreach ($verifygoodlogs as &$verifygoodlog) 
						{
							$verifygoodlog['verifydate']=date('Y-m-d H:i:s',$verifygoodlog['verifydate']);
							$verifynum += intval($verifygoodlog['verifynum']);
						}
						$gvv['verifygoodlogs']=$verifygoodlogs;
						$gvv['lastverifys'] = intval($gvv['limitnum']) - $verifynum;
					}
					if (empty($gvv['limittype'])) {
							$gvv['limitdays']=$gvv['limitdays']?:365;
							$limitdate = intval($gvv['starttime']) + (intval($gvv['limitdays']) * 86400);
					}else{
							$limitdate = intval($gvv['limitdate']);
					}
					if ($limitdate < time()) {
							$gvv['status']=3;
					}
					$code_endtime=$limitdate?date('Y-m-d',$limitdate):'';
					$gvv['url']=custom_url('jy_weishop/verify.verifygoodsdetail//id='.$order['id'].'&verifycode='.$gvv['verifycode'],true);
					//小程序二维码：
					if($_SESSION['fromh5wxapp']){
						//不能超128字符
						$url='/jy_weishop/verify/verifygoodsdetail/index?id='.$order['id'].'&verifycode='.$gvv['verifycode'];
						$url=urlencode($url);
						$url='/pages/html/html?url='.$url;
					}else{
						$url='/jy_weishop/verify/verifygoodsdetail/index?id='.$order['id'].'&verifycode='.$gvv['verifycode'];
					}
					$pid='verifygoodsid'.$order['id'].'c'.$gvv['verifycode'];
					$gvv['appcode'] = $this->model('model')->get_wxappewm($url,$pid);
				}
		}
		$return['goods_verify']=$goods_verify;
		$return['code_endtime']=$code_endtime;
		if(count($return['virtual']) || count($return['goods_verify']) || count($return['order_verify'])){
			$order['isverify']=2;
		}
		if($order['paytype']){
			$order['paytypetitle']=get_pay_types()[$order['paytype']];
		}
		$order['paylog']=pdo_get('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['pay_id']));
		if($order['paylog']['di_fee']>0){
			$credit_types=credit_types(1,1);
			$order['paylog']['difeetitle']=$credit_types[$order['paylog']['di_credit']].'抵扣';
			$order['price']=$order['price']-$order['paylog']['di_fee'];
		}
		$order['paydiyfields']=$order['paylog']['diyfields'];
		$order['paydiyfields']=json_decode($order['paydiyfields']);
		$return['order']=$order;
		$return['coupon_wait']=$this->model('task')->pop_coupon_wait();
		$return['coupon_wait']=$return['coupon_wait']?$return['coupon_wait'][0]:false;
		if(!$return['coupon_wait']){
				$return['coupon_hd']=$this->model('task')->pop_coupon_hd();
				if(!$return['coupon_hd']){
					$return['game_wait']=$this->model('task')->pop_game();
				}
		}
		//拼团
		$return['go_goodsitem']=$order['is_team']?'jy_pingdd':'jy_weishop';
		$return['post']=array('libaos'=>'');
		return $return;
	}
}